package com.itheima.exerciseTest.method10_19.method11;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.TreeSet;

/*
    分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序，
    如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
    姓名（String）	年龄（int）	分数（float）
    liusan	        20	        90.0F
    lisi		    22	        90.0F
    wangwu		    20	        99.0F
    sunliu		    22	        100.0F
 */
public class ComparatorTest {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // 中文比较器
                Collator ins = Collator.getInstance(Locale.CHINESE);
                // 比较成绩
                int result = Float.compare(s1.getGrade(), s2.getGrade());
                // 比较年龄
                result = result == 0 ? s1.getAge() - s2.getAge() : -result;
                if (result != 0) {
                    return result;
                }
                // 比较名字
                if (result == 0) {
                    char[] chars = s1.getName().toCharArray();
                    boolean boo = false;
                    // 判断传入字符是中文字符还是英文子符
                    for (int i = 0; i < chars.length; i++) {
                        if (chars[i] >= 'A' && chars[i] <= 'Z') {
                            boo = true;
                            break;
                        } else if (chars[i] >= 'a' && chars[i] <= 'z') {
                            boo = true;
                            break;
                        }
                    }
                    if (boo == true) {
                        result = s1.getName().compareTo(s2.getName());
                        return result;
                    } else {
                        result = ins.compare(s1.getName(), s2.getName());
                    }
                }
                return result;
            }
        });

        Student s1 = new Student("liusan", 20, 99.0F);
        Student s2 = new Student("lisi", 22, 90.0F);
        Student s3 = new Student("wangwu", 20, 99.0F);
        Student s4 = new Student("sunliu", 22, 100.0F);
        Student s5 = new Student("张三", 22, 90.0F);
        Student s6 = new Student("李四", 22, 90.0F);
        Student s7 = new Student("王五", 22, 100.0F);
        Student s8 = new Student("李四", 22, 100.0F);
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        ts.add(s7);
        ts.add(s8);
        for (Student stu : ts) {
            System.out.println(stu);
        }
    }
}
